//
//  HNViewController.m
//  XDEducation
//
//  Created by 星道 on 17/5/18.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNViewController.h"
#import "HNKnowledgeModel.h"
#import "HNCleanProblemView.h"
#import "HNGetKnowledgePapersModel.h"
#import "HNGetKnowledgePapers.h"
@interface HNViewController ()
{
    IBOutlet UILabel *_KnowledgeLabel;
    IBOutlet UILabel *_TeacherLabel;
    IBOutlet UITableView *_TableView;
    
}
@property (nonatomic, strong) NSMutableArray *KnowledgeArr;
@property (nonatomic, strong) NSMutableArray *TeacherArr;
@property (nonatomic, strong) NSMutableArray *PapersArr;
@property (nonatomic, assign) NSInteger SelectRow;
@property (nonatomic, strong) HNKnowledgeModel *TempModel;
@property (nonatomic, strong) HNGetKnowledgePapers *Papers;
@end

@implementation HNViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"清除盲点";
    
    [self RefreshData:NO];
}




#pragma mark - 网络请求
- (void)RefreshData:(BOOL)IsTeacher
{
    [MBProgressHUD showHUD];
    
    NSString *UrlStr = BaseURL([self Action:IsTeacher]);
    [XDHttpTool get:UrlStr authCode:[AccountTool shareAccountTool].currentAccount.authCode params:nil success:^(id responseObj) {
        if ([responseObj count] > 0)
        {
            
            NSArray *Arr = nil;
            if (IsTeacher && [responseObj[@"code"] isEqualToNumber:@1000])
            {
                Arr = (NSArray *)responseObj[@"t"];
            }
            else
            {
                Arr = (NSArray *)responseObj;
            }
            
            for (NSDictionary *Dict in Arr)
            {
                if (IsTeacher)
                {
                    HNKnowledgeModel *Model = [[HNKnowledgeModel alloc] initWithDict:Dict];
                    Model.ID = [self.KnowledgeArr[self.SelectRow] ID];
                    [self.TeacherArr addObject:Model];
                }
                else
                {
                    
                    [self.KnowledgeArr addObject:[[HNKnowledgeModel alloc] initWithDict:Dict]];
                }
            }
            [self OpenTableView:IsTeacher];
        }
        else
        {
            [MBProgressHUD showMessage:XDNoData];
        }
    } failure:^(NSError *error) {
        
        [MBProgressHUD showError:XDNetWorkError];
    }];
}



/** 查找 */
- (IBAction)LookUp:(UIButton *)sender
{
    [MBProgressHUD showHUD];
    NSString *UrlStr = [NSString stringWithFormat:@"consolidate.do?action=getKnowledgePapers&uid=%@&knowledgeId=%@&teacherId=%@",[AccountTool shareAccountTool].currentAccount.uid,[self.TempModel.ID stringValue],[self.TempModel.uid stringValue]];
    [XDHttpTool get:BaseURL(UrlStr) authCode:[AccountTool shareAccountTool].currentAccount.authCode params:nil success:^(id responseObj) {
        if ([responseObj[@"code"] isEqualToNumber:@1000] && [responseObj[@"t"][@"dataList"] count] > 0)
        {
            [self.PapersArr removeAllObjects];
            for (NSDictionary *dict in responseObj[@"t"][@"dataList"])
            {
                HNGetKnowledgePapersModel *Model = [[HNGetKnowledgePapersModel alloc] init_HNObjectForKey:dict];
                [self.PapersArr addObject:Model];
            }
            [self.Papers Arr:self.PapersArr];
        }
        else
        {
            [MBProgressHUD showError:@"暂无数据"];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:XDNetWorkError];
    }];
}



#pragma mark - Fuc
/** 请求地址 */
- (NSString *)Action:(BOOL)IsTeacher
{
    if (IsTeacher)
    {
        HNKnowledgeModel *Model = self.KnowledgeArr[self.SelectRow];
        return [NSString stringWithFormat:@"consolidate.do?action=getTeachers&num=%@&type=3",[Model.ID stringValue]];
    }
    else
    {
        return [NSString stringWithFormat:@"myresult.do?action=getKnByknid&knowledgeid=%@",self.KnowledgeID];
    }
}

/** 打开选择表单 */
- (void)OpenTableView:(BOOL)IsTeacher
{
    if (self.KnowledgeArr.count > 0)
    {
        HNCleanProblemView *View = [[HNCleanProblemView alloc] initWithDataArr:IsTeacher?self.TeacherArr:self.KnowledgeArr IsTeacher:IsTeacher SelectRow:^(NSInteger Index,NSString *Text) {
            self.SelectRow = Index;
            if (IsTeacher)
            {
                self.TempModel = self.TeacherArr[Index];
                _TeacherLabel.text = Text;
            }
            else
            {
                self.TempModel = self.KnowledgeArr[Index];
                _KnowledgeLabel.text = Text;
            }
        }];
        [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:View];
    }
}

#pragma mark - Action
- (IBAction)SelectKnowledge:(UIButton *)sender
{
    if ([self.KnowledgeArr count] > 0)
    {
    [self OpenTableView:NO];
    }
    else
    {
        [self RefreshData:NO];
    }
}

- (IBAction)SelectTeacher:(UIButton *)sender
{
    if (self.TempModel == nil)
    {
        [MBProgressHUD showError:@"请先选择知识点"];
        return;
    }
    
    if ([self.TeacherArr count] > 0)
    {
        [self OpenTableView:YES];
    }
    else
    {
        [self RefreshData:YES];
    }
}



#pragma mark - Setter
- (NSMutableArray *)KnowledgeArr
{
    if (!_KnowledgeArr)
    {
        _KnowledgeArr = [NSMutableArray array];
        
    }
    return _KnowledgeArr;
}

- (NSMutableArray *)TeacherArr
{
    if (!_TeacherArr)
    {
        _TeacherArr = [NSMutableArray array];
    }
    return _TeacherArr;
}

- (NSMutableArray *)PapersArr
{
    if (!_PapersArr)
    {
        _PapersArr = [NSMutableArray array];
    }
    return _PapersArr;
}

- (HNGetKnowledgePapers *)Papers
{
    if (!_Papers)
    {
        _Papers = [[HNGetKnowledgePapers alloc] init_TableView:_TableView];
    }
    return _Papers;
}
@end
